#!/bin/sh

# Compare the symbol usage between the MetadataStore and
# MetadataRetrieve interfaces and the MetadataConverter class.  This
# will indicate if any symbols are not being converted.

set -e

# Metadata store and retrieve symbols
store_symbols()
{
    grep "^ *get" "@PROJECT_BINARY_DIR@/cpp/lib/ome/xml/meta/MetadataRetrieve.h" | \
      sed -e 's;^ *\(get[a-zA-Z0-9][a-zA-Z0-9]*\).*;\1;'
    grep "^ *set" "@PROJECT_BINARY_DIR@/cpp/lib/ome/xml/meta/MetadataStore.h" | \
       sed -e 's;^ *\(set[a-zA-Z0-9][a-zA-Z0-9]*\).*;\1;'
}

# Metadata store and retrieve symbols used in MetadataConvert
convert_symbols()
{
	nl='\
'
    egrep "&M[RS]::|src\." "@PROJECT_SOURCE_DIR@/cpp/lib/ome/xml/meta/Convert.cpp" | sed -e "s;&M[RS]::;$nl;g" -e "s;src\.;$nl;g" | \
      grep "^[gs]et" | \
      sed -e "s;^\([gs]et[a-zA-Z0-9][a-zA-Z0-9]*\).*;\1;"
}

saved_store=$(mktemp bf-store-symbols.XXXXXXXXX)
saved_convert=$(mktemp bf-convert-symbols.XXXXXXXXX)
trap "rm -f \"$saved_store\" \"$saved_convert\"" 0 1 2 3 15

store_symbols | sort | uniq | egrep -v '^(setRoot)$' >> "$saved_store"
convert_symbols | sort | uniq >> "$saved_convert"

missing_convert="$(comm -2 -3 "$saved_store" "$saved_convert")"
missing_store="$(comm -1 -3 "$saved_store" "$saved_convert")"

status=0

if [ -n "$missing_store" ]; then
    echo "Missing symbols in MetadataStore (likely a bug in this parser):"
    echo "$missing_store"
    status=2
fi
if [ -n "$missing_convert" ]; then
    echo "Missing symbols in MetadataConverter:"
    echo "$missing_convert"
    status=1
fi

exit $status
